package com.codedancer.campus.mapper;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.core.BaseMapper;
import com.codedancer.campus.enums.OrderType;
import com.codedancer.campus.enums.CommodityPriceRegion;
import com.codedancer.campus.enums.CommodityStatus;
import com.codedancer.campus.po.Commodity;
import com.codedancer.campus.qo.commodity.CommodityPageSearchQo;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface CommodityMapper extends BaseMapper<Commodity> {

    /**
     * 分页获取用户发布商品
     *
     * @param commodityPage
     * @param commodityPageSearchQo
     * @return
     */
    default IPage<Commodity> pageSearchUserCommodity(Page<Commodity> commodityPage,
        CommodityPageSearchQo commodityPageSearchQo) {
        LambdaQueryWrapper<Commodity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Commodity::getPublishUserUuid,
            commodityPageSearchQo.getCurrentUserUuid());
        if (ObjectUtil.isNotEmpty(commodityPageSearchQo.getTitle())) {
            lambdaQueryWrapper.like(Commodity::getTitle, commodityPageSearchQo.getTitle());
        }
        lambdaQueryWrapper.orderByDesc(Commodity::getCreateTime);
        return selectPage(commodityPage, lambdaQueryWrapper);
    }

    /**
     * 分页查询商品(需要获取已上架的商品)
     *
     * @param commodityPage
     * @param commodityPageSearchQo
     * @return
     */
    default IPage<Commodity> pageSearch(Page<Commodity> commodityPage,
        CommodityPageSearchQo commodityPageSearchQo) {
        LambdaQueryWrapper<Commodity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(commodityPageSearchQo.getTitle())) {
            lambdaQueryWrapper.like(Commodity::getTitle, commodityPageSearchQo.getTitle());
        }
        if (ObjectUtil.isNotEmpty(commodityPageSearchQo.getCategoryUuid())
            && !commodityPageSearchQo.getCategoryUuid().equals("all")) {
            lambdaQueryWrapper.eq(Commodity::getCategoryUuid,
                commodityPageSearchQo.getCategoryUuid());
        }
        // 默认排序或者是按照时间降序
        if (commodityPageSearchQo.getOrderType().equals(OrderType.DEFAULT.getValue()) ||
            commodityPageSearchQo.getOrderType().equals(OrderType.TIME_DESC.getValue())) {
            lambdaQueryWrapper.orderByDesc(Commodity::getCreateTime);
        }
        // 按照时间升序
        if (commodityPageSearchQo.getOrderType().equals(OrderType.TIME_ASC.getValue())) {
            lambdaQueryWrapper.orderByAsc(Commodity::getCreateTime);
        }
        // 价格在0~50
        if (ObjectUtil.isNotEmpty(commodityPageSearchQo.getPriceRegion())) {
            if (commodityPageSearchQo.getPriceRegion()
                .equals(CommodityPriceRegion.ZERO_TO_FIFTY.getValue())) {
                lambdaQueryWrapper.ge(Commodity::getCurrentPrice, 0);
                lambdaQueryWrapper.le(Commodity::getCurrentPrice, 50);
            }
            // 价格在50～100
            if (commodityPageSearchQo.getPriceRegion()
                .equals(CommodityPriceRegion.FIFTY_TO_HUNDRED.getValue())) {
                lambdaQueryWrapper.ge(Commodity::getCurrentPrice, 50);
                lambdaQueryWrapper.le(Commodity::getCurrentPrice, 100);
            }
            // 价格在100~500
            if (commodityPageSearchQo.getPriceRegion()
                .equals(CommodityPriceRegion.HUNDRED_TO_FIVE_HUNDRED.getValue())) {
                lambdaQueryWrapper.ge(Commodity::getCurrentPrice, 100);
                lambdaQueryWrapper.le(Commodity::getCurrentPrice, 500);
            }
            // 价格在500+
            if (commodityPageSearchQo.getPriceRegion()
                .equals(CommodityPriceRegion.FIVE_HUNDRED_ABOVE.getValue())) {
                lambdaQueryWrapper.gt(Commodity::getCurrentPrice, 500);
            }
        }
        lambdaQueryWrapper.eq(Commodity::getStatus, CommodityStatus.ON_SHELF);
        lambdaQueryWrapper.eq(Commodity::getPublishSchoolUuid,
            commodityPageSearchQo.getPublishSchoolUuid());
        return selectPage(commodityPage, lambdaQueryWrapper);
    }

    /**
     * 分页查询商品(用于管理端)
     *
     * @param commodityPage
     * @param commodityPageSearchQo
     * @return
     */
    IPage<Commodity> pageToManager(Page<Commodity> commodityPage,
        @Param("commodityPageSearchQo") CommodityPageSearchQo commodityPageSearchQo);

    /**
     * 商品详情
     *
     * @param uuid
     * @return
     */
    Commodity detail(String uuid);

    /**
     * 获取导出的商品数据
     * @param commodityPageSearchQo
     * @return
     */
    List<Commodity> listExportCommodity(@Param("commodityPageSearchQo") CommodityPageSearchQo commodityPageSearchQo);

    /**
     * 获取商品-根据条件判断
     * @param commodityPageSearchQo
     * @return
     */
    default List<Commodity> getCommodityList(CommodityPageSearchQo commodityPageSearchQo){
        LambdaQueryWrapper<Commodity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(commodityPageSearchQo.getStatus())) {
            lambdaQueryWrapper.eq(Commodity::getStatus, commodityPageSearchQo.getStatus());
        }
        // 默认排序或者是按照时间降序
        if (commodityPageSearchQo.getOrderType().equals(OrderType.DEFAULT.getValue()) ||
                commodityPageSearchQo.getOrderType().equals(OrderType.TIME_DESC.getValue())) {
            lambdaQueryWrapper.orderByDesc(Commodity::getCreateTime);
        }
        // 按照时间升序
        if (commodityPageSearchQo.getOrderType().equals(OrderType.TIME_ASC.getValue())) {
            lambdaQueryWrapper.orderByAsc(Commodity::getCreateTime);
        }
        return selectList(lambdaQueryWrapper);
    }
}
